package com.test.group6.controller;

import java.util.ArrayList;
import java.util.List;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.mysql.fabric.xmlrpc.base.Array;
import com.test.group6.domain.EnterpriseIds;
import com.test.group6.domain.WorkesInformation;
import com.test.group6.service.DeleteEnterpriseService;
import com.test.group6.service.DeleteWorkesService;


/**
 * Title EnterpriseController.java
 * Description 企业基本信息维护controller层
 * @author leng_qqing
 * @date 2021年7月23日
 * @version 1.0
 */
@RestController
@RequestMapping("/delectEnterpriseController")
public class DelectEnterpriseController {
	@Autowired
	DeleteEnterpriseService deleteEnterpriseService;
	@Autowired
	DeleteWorkesService deleteWorkesService;


	/**
	 * @Description 单条删除企业基本信息，同时删除其高管信息
	 * @param enterpriseId 企业编号
	 * @return 操作执行结果，成功返回success，失败返回error
	 */
	@RequestMapping("/deleteEnterprise")	
	public String deleteEnterpriseByEnterpriseId(String enterpriseId) {
		//根据企业编号删除企业基本信息
		int enterpriseCount = deleteEnterpriseService.deleteEnterprise(enterpriseId);
		if (enterpriseCount > 0) {
			//查询企业高管是否存在
			List<WorkesInformation> workes = deleteWorkesService.selectWorkes(enterpriseId);
			//判断企业高管集合是否为空
			if (workes != null && !workes.isEmpty()) {
				//根据企业编号删除企业高管信息
				int workerCount = deleteWorkesService.deleteWorkesByEnterpriseId(enterpriseId);
				if (workerCount > 0) {
					return "success";
				} else {
					return "error";
				}
			}

		} else {
			return "error";
		}
		return "success";
	}


	/**
	 * @Description 批量删除企业基本信息，同时删除其高管信息
	 * @param enterpriseId 企业编号数组
	 * @return 操作执行结果，成功返回success，失败返回error
	 */
	@RequestMapping("/deleteBatchEnterprise")
	public String deleteBatchEnterprise(EnterpriseIds enterpriseId) {
		
		System.out.println(enterpriseId);
	System.out.println(enterpriseId.getEnterpriseId()[0].length());
	System.out.println(enterpriseId.getEnterpriseId()[1]);
	System.out.println(enterpriseId.getEnterpriseId()[2]);

//	for(int i=0;i<enterpriseId.length;i++) {
//		if(i==0) {
//			for( )
//		}
//	}
		int i = 0;
		//循环遍历企业编号数组
		while (i < enterpriseId.getEnterpriseId().length) {
			//根据企业编号删除企业信息
			int enterpriseCount = deleteEnterpriseService.deleteEnterprise(enterpriseId.getEnterpriseId()[i]);
			
			if (enterpriseCount > 0) {
				//查询企业高管是否存在
				List<WorkesInformation> workes = deleteWorkesService.selectWorkes(enterpriseId.getEnterpriseId()[i]);
				//判断企业高管集合是否为空
				if (workes != null && !workes.isEmpty()) {
					System.out.println("进入员工");
					//根据企业编号删除企业高管信息
					int workerCount = deleteWorkesService.deleteWorkesByEnterpriseId(enterpriseId.getEnterpriseId()[i]);
					if (workerCount > 0) {
						i++;
						continue;
					} else {
						System.out.println("删员工");
						return "error";
					}
				}
				i++;
			} else {
				System.out.println("删企业");
				return "error";
			}
		}
		return "success";
	}

	

}
